<template>

  <a-drawer
    :title="title"
    width="50%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @close="handleCancel"
    cancelText="关闭"
    style="height: 100%;overflow: auto;padding-bottom: 53px;">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <template v-if="title==='新增'">
          <a-form-item label="门店" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <!--<j-select-terminal placeholder="请选择门店" v-decorator="['terminals', validatorRules.terminals]"-->
                               <!--:trigger-change="true"></j-select-terminal>-->
            <app-stores placeholder="请选择门店" @select="selects"
                        v-decorator="['terminalCode', validatorRules.terminalCode]"
                        :trigger-change="true"></app-stores>
          </a-form-item>
          <CodeConfigModal
            modal-width="90%"
            :showCode="this.showCode"
            ref="CodeConfigModal"
            @ok="handelSaveOk">
          </CodeConfigModal>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" hasFeedback label="归属经销商名">
            <a-input placeholder="经销商名称" readonly v-model="custName"/>
            <a-button icon="search" @click="handleToCust"></a-button>
          </a-form-item>
          <a-form-item label="当前信用余额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="0" v-decorator="['initQuota', {}]"/>
          </a-form-item>
        </template>

        <template v-if="title==='调整'">
          <a-form-item label="门店编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入门店编号" disabled v-decorator="['terminalCode', validatorRules.terminalCode]"
                     readOnly/>
          </a-form-item>
          <a-form-item label="门店名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入门店名称" disabled v-decorator="['terminalName', validatorRules.terminalName]"
                     readOnly/>
          </a-form-item>
          <a-form-item label="经销商编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入经销商编号" disabled v-decorator="['custCode', validatorRules.custCode]"
                     readOnly/>
          </a-form-item>
          <a-form-item label="经销商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入经销商名称" disabled v-decorator="['custName', validatorRules.custName]"
                     readOnly/>
          </a-form-item>
          <a-form-item label="当前信用余额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="0" v-decorator="['usableQuota', {}]" readOnly/>
          </a-form-item>
          <a-form-item label="本次调整值" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="0" v-decorator="['adjustQuota', validatorRules.currentAmount]" @change="inpteValue"/>
          </a-form-item>
          <a-form-item label="调整后余额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="0" v-model="quota" readOnly
            />
          </a-form-item>
          <a-form-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="备注">
            <a-textarea placeholder="请输入备注" v-decorator="['remarks', validatorRules.remarks]"/>
          </a-form-item>
        </template>

      </a-form>

    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃调整？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleOk" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>

</template>

<script>
  import CodeConfigModal from '@/components/cn-select-popup/CodeConfigModal'

  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import AFormItem from 'ant-design-vue/es/form/FormItem'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JImageUpload from '@/components/cn-form/JImageUpload'
  import JTreeSelect from '@/components/cn-form/JTreeSelect'
  import JSelectUserByDep from '@/components/cn-select-popup/JSelectUserByDep'
  import JSelectTerminal from '@/components/cn-select-popup/JSelectTerminal'

  export default {
    name: 'creditModal',
    components: {
      JImageUpload,
      AFormItem,
      ATextarea,
      JTreeSelect,
      JSelectUserByDep,
      JSelectTerminal,

      CodeConfigModal
    },
    data() {
      return {
        quota:0,
        custCode: '',
        custName: '',
        showCode: "选择经销商",
        terminalName: '',
        terminalCode: '',
        title: "操作",
        visible: false,
        selecttype: '',
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,

        form: this.$form.createForm(this),
        disableSubmit:false,
        validatorRules: {
          terminals: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          terminalCode: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          terminalName: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          custCode: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          custName: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          currentAmount: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          },
          remarks: {
            rules: [{
              required: true, message: '', trigger: 'submit'
            }]
          }
        },
        url: {
          add: window._CONFIG['domianURL'] + "/credit/cnCreditHead/save",
          edit: window._CONFIG['domianURL'] + "/credit/cnCreditHead/adjustment",
        },
      }
    },
    methods: {
      inpteValue(){
        this.$nextTick(() => {
        this.form.validateFields((err, values) => {
          this.quota=Number(this.model.usableQuota) + Number(values.adjustQuota)
           })
        })
      },
      handelSaveOk(value) {
        this.custName = value.name
        this.custCode = value.code
      },
      handleToCust() {
        this.$refs.CodeConfigModal.showModal()
      },
      selects(value) {
        this.terminalName = value[0].terminalName
        this.terminalCode = value[0].terminalCode
      },
      dotype(val) {
        this.selecttype = val
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.isRoll = this.model.isRoll + "";
        this.model.isControl = this.model.isControl + "";
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'terminalCode', 'terminalName',
            'custCode','custName','amount','usableQuota'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let formData = Object.assign(this.model, values);
            if (this.title == "新增") {
              httpurl += this.url.add;
              formData.terminalName = this.terminalName
              formData.terminalCode = this.terminalCode
              formData.custCode = this.custCode
              formData.custName = this.custName
              formData.type = 0;
            } else if (this.title == "调整") {
              httpurl += this.url.edit;
              formData.type = 1;
            }
            //时间格式化
            httpAction(httpurl, formData, 'post').then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style scoped>
  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>